﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace SoftWareLab_Project
{
    public partial class ShowTotalWork : System.Web.UI.Page
    {
        SertData sert = new SertData();
        private double a = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"]!=null)
                {
                    if (Session["P_MANAGEMENT"].ToString() == "หัวหน้าแผนก")
                    {
                        txtSertFName.Visible = false;
                        Label1.Visible = false;
                        lb2.Visible = false;
                        lb3.Visible = false;
                        DropDownList1.Visible = false;
                        DropDownList2.Visible = false;
                        btSert.Visible = false;
                        BindingGridHead(Session["ID_DEPARTMENT"].ToString());
                    }
                    else if (Session["P_MANAGEMENT"].ToString() == "อาจารย์")
                    {
                        txtSertFName.Visible = false;
                        Label1.Visible = false;
                        lb2.Visible = false;
                        lb3.Visible = false;
                        DropDownList1.Visible = false;
                        DropDownList2.Visible = false;
                        btSert.Visible = false;
                        BindingGridTeach(Session["User"].ToString());
                    }
                    else
                    {
                        txtSertFName.Visible = true;
                        Label1.Visible = true;
                        lb2.Visible = true;
                        lb3.Visible = true;
                        btSert.Visible = true;
                        DropDownList1.Visible = true;
                        DropDownList2.Visible = true;
                        BindingGrid();
                    } 
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
               
            }
        }
        
        private void BindingGrid()
        {

            DataTable dt = sert.GetData();
            Gv_showData.DataSource = dt;
            Gv_showData.DataBind();
            if (dt == null)
            {
                lbWarnNo.Visible = true;
            }


        }
        private void BindingGridHead(string Depart)
        {

            DataTable dt = sert.GetDataHead(Depart);
            Gv_showData.DataSource = dt;
            Gv_showData.DataBind();
            if (dt == null)
            {
                lbWarnNo.Visible = true;
            }
        }
        private void BindingGridTeach(string User)
        {

            DataTable dt = sert.GetDataFromUsername(User);
            Gv_showData.DataSource = dt;
            Gv_showData.DataBind();
            if (dt == null)
            {
                lbWarnNo.Visible = true;
            }
        }

        protected void btSert_Click(object sender, EventArgs e)
        {
            if (Session["User"]!= null)
            {
                DataTable dt = new DataTable();
                lbWarnNo.Visible = false;
                if (txtSertFName.Text != "" && Convert.ToInt32(DropDownList1.SelectedValue) == 0 && Convert.ToInt32(DropDownList2.SelectedValue) == 0)
                {
                    dt = sert.GetDataFromName(txtSertFName.Text);
                }
                if (Convert.ToInt32(DropDownList2.SelectedValue) == 0 && txtSertFName.Text != "" && Convert.ToInt32(DropDownList1.SelectedValue) != 0)
                {
                    dt = sert.GetDataFromNameFac(txtSertFName.Text, Convert.ToInt32(DropDownList2.SelectedValue));
                }
                if (Convert.ToInt32(DropDownList1.SelectedValue) == 0 && txtSertFName.Text != "" && Convert.ToInt32(DropDownList2.SelectedValue) != 0)
                {
                    dt = sert.GetDataFromNameDep(txtSertFName.Text, Convert.ToInt32(DropDownList2.SelectedValue));
                }
                if (txtSertFName.Text == "" && Convert.ToInt32(DropDownList2.SelectedValue) == 0 && Convert.ToInt32(DropDownList1.SelectedValue) != 0)
                {
                    dt = sert.GetDataFromFac(Convert.ToInt32(DropDownList1.SelectedValue));
                }
                if (txtSertFName.Text == "" && Convert.ToInt32(DropDownList1.SelectedValue) == 0 && Convert.ToInt32(DropDownList2.SelectedValue) != 0)
                {
                    dt = sert.GetDataDepartment(Convert.ToInt32(DropDownList2.SelectedValue));
                }
                if (txtSertFName.Text != "" && Convert.ToInt32(DropDownList2.SelectedValue) != 0 && Convert.ToInt32(DropDownList1.SelectedValue) != 0)
                {
                    dt = sert.GetDataFromAll(txtSertFName.Text, Convert.ToInt32(DropDownList1.SelectedValue), Convert.ToInt32(DropDownList2.SelectedValue));
                }
                if (txtSertFName.Text == "" && Convert.ToInt32(DropDownList2.SelectedValue) != 0 && Convert.ToInt32(DropDownList1.SelectedValue) != 0)
                {
                    dt = sert.GetDataFromFacDep(Convert.ToInt32(DropDownList1.SelectedValue), Convert.ToInt32(DropDownList2.SelectedValue));
                }
                if (!(dt != null && dt.Rows.Count > 0))
                {
                    lbWarnNo.Visible = true;
                }
                Gv_showData.DataSource = dt;
                Gv_showData.DataBind();
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
           
            
        }
        
        protected void Gv_showData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdDetail")
            {
                int i = Convert.ToInt32(e.CommandArgument.ToString());
                Session["DetailNo_id"] = i;
                Server.Transfer("DetailTotalWork.aspx");

            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList2.Items.Clear();
        }   
    }
}